package controller;

import java.io.File;
import java.io.Serializable;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.SessionScoped;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import model.Metric;
import model.Metric_User;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

@SessionScoped
public class XmlReader {

    private String xmlPathname;

    public XmlReader(String path) {
        xmlPathname = path;
    }

    public List<Metric_User> lerMetric() throws Exception {


        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        final DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(xmlPathname));

        Element elem = doc.getDocumentElement();
        NodeList nl = elem.getElementsByTagName("metric");
        List<Metric_User> listMetric = new ArrayList<Metric_User>();

        // percorre cada elemento usuario encontrado
        for (int i = 0; i < nl.getLength(); i++) {
            Element tagUsuario = (Element) nl.item(i);

            // pega os dados cadastrado para a metrica atual
            String name = tagUsuario.getAttribute("name");
            System.out.println("NOME>>>" + name);
            String domain = tagUsuario.getAttribute("domain");
            System.out.println("DOmain" + domain);
            String description = getChildTagValue(tagUsuario, "description");
            String type = tagUsuario.getAttribute("category");

            // cria uma nova instancia do metric com os dados do xml
            Metric_User metric = new Metric_User(name, description, domain, type);

            // adiciona o metric na lista de metricas
            listMetric.add(metric);
        }

        return listMetric;
    }

    public List<Metric> lerMetricAdmin() throws Exception {
//        String Data = 	The number of attributes in the class.</description><projection relation="+context+" target="+attribute+" /></metric>";
                
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(xmlPathname);

        doc.getDocumentElement().normalize();

        NodeList nList = doc.getElementsByTagName("metric");
                
        List<Metric> listMetric = new ArrayList<Metric>();

        // percorre cada elemento usuario encontrado
        for (int i = 0; i < nList.getLength(); i++) {
            
            Node nNode = nList.item(i);
            
            Element tagUsuario = (Element) nNode;

            // pega os dados cadastrado para a metrica atual
            String name = tagUsuario.getAttribute("name");
            String domain = tagUsuario.getAttribute("domain");
            String description = getChildTagValue(tagUsuario, "description");
            String type = tagUsuario.getAttribute("category");
            // cria uma nova instancia do metric com os dados do xml
            Metric metric = new Metric(name, description, domain, type);

            // adiciona o metric na lista de metricas
            listMetric.add(metric);
        }
        return listMetric;
    }

    private String getChildTagValue(Element elem, String tagName) throws Exception {
        NodeList children = elem.getElementsByTagName(tagName);
        if (children == null) {
            return null;
        }
        Element child = (Element) children.item(0);
        if (child == null) {
            return null;
        }
        return child.getFirstChild().getNodeValue();
    }
}
